package org.xxpay.manage.sys.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xxpay.core.common.annotation.MethodLog;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.BizResponse;
import org.xxpay.core.common.domain.XxPayPageRes;
import org.xxpay.core.common.domain.XxPayResponse;
import org.xxpay.core.entity.SysQuota;
import org.xxpay.manage.common.ctrl.BaseController;
import org.xxpay.manage.common.service.RpcCommonService;
import org.xxpay.manage.secruity.JwtUser;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

@Controller
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/sys/quota")
public class SysQuotaController extends BaseController {

    @Autowired
    private RpcCommonService rpcCommonService;
    /**
     * 查询
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        SysQuota sysQuota = getObject(param, SysQuota.class);

        List<SysQuota> data = rpcCommonService.iSysQuotaService.select((getPageIndex(param) -1) * getPageSize(param), getPageSize(param),sysQuota);
        int count = rpcCommonService.iSysQuotaService.count();
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(data, count));
    }

    /**
     * 系统额度总点数查询
     * @return
     */
    @RequestMapping("/getAmountCount")
    @ResponseBody
    public ResponseEntity<?> getAmountCount(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        SysQuota sysQuota = getObject(param, SysQuota.class);
        Double count = rpcCommonService.iSysQuotaService.getAmountCount();
        return ResponseEntity.ok(XxPayResponse.buildSuccess(count));
    }

    /**
     * 新增系统额度
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    @MethodLog( remark = "新增系统额度" )
    public ResponseEntity<?> add(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        String ipAddr = getIpAddr(request);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser jwtUser = (JwtUser) authentication.getPrincipal();
        SysQuota sysQuota = getObject(param, SysQuota.class);
        sysQuota.setIp(ipAddr);
        sysQuota.setUser_id(jwtUser.getId());
        sysQuota.setUser_name(jwtUser.getUsername());
        int count = rpcCommonService.iSysQuotaService.add(sysQuota);
        if(count != 1) ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
        return ResponseEntity.ok(BizResponse.buildSuccess());
    }

    /**
     * 清空系统额度
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @MethodLog( remark = "清空系统额度" )
    public ResponseEntity<?> delete() {
        int count = rpcCommonService.iSysQuotaService.delete();
        if(count != 1) ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
        return ResponseEntity.ok(BizResponse.buildSuccess());
    }


    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if ((ip.indexOf(",") >= 0)) {
            ip = ip.substring(0, ip.indexOf(","));
        }
        return ip;
    }
}
